﻿using LCL.CodeHelper;
using LCL.IBuilder;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.Builder.IBatisNet
{
    public partial class ProjectBuilder
    {
        #region BuilderIDAL
        private void BuilderIDAL()
        {
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                var Fieldlist = DbObject.GetColumnInfoList(DbName, table.TableName);
                List<TableColumn> Keys = Fieldlist.Where(p => p.IsPrimaryKey == true).ToList();
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                string ModelSpace = NameSpace + ".Model." + className;//命名空间 + 类名
                string DALSpace = NameSpace + ".DAL." + className;
                string IDALSpace = NameSpace + ".IDAL." + className;

                StringBuilder builder = new StringBuilder();
                builder.AppendLine("using System; ");
                builder.AppendLine("using System.Collections.Generic; ");
                builder.AppendLine("using System.Data; ");
                builder.AppendLine("namespace " + NameSpace + ".IDAL");
                builder.AppendLine("{ ");
                builder.AppendLine("    public interface I" + className + "DAL ");
                builder.AppendLine("    { ");

                builder.AppendLine("        int Add(" + ModelSpace + " model); ");
                builder.AppendLine("        bool Delete(" + CodeCommon.GetInParameter(Keys) + "); ");
                builder.AppendLine("        bool Exists(" + CodeCommon.GetInParameter(Keys) + "); ");
                builder.AppendLine("        DataTable GetList(string strWhere); ");
                builder.AppendLine("        List<" + ModelSpace + "> GetListModel(string strWhere); ");
                builder.AppendLine("        " + ModelSpace + " GetModel(" + CodeCommon.GetInParameter(Keys) + "); ");
                builder.AppendLine("        bool Update(" + ModelSpace + " model); ");

                builder.AppendLine("    } ");
                builder.AppendLine("} ");
                string filename = CodeCommon.DesktopDirectory + NameSpace + @"/IDAL/" + className + ".cs";
                CodeCommon.CreateFiles(filename, builder.ToString());
            }
            IDALCsproj();
        }
        private void IDALCsproj()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
            builder.AppendLine("<Project DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" ToolsVersion=\"4.0\"> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <ProjectType>Local</ProjectType> ");
            builder.AppendLine("    <ProductVersion>8.0.50727</ProductVersion> ");
            builder.AppendLine("    <SchemaVersion>2.0</SchemaVersion> ");
            builder.AppendLine("    <ProjectGuid>{" + IDALGuid + "}</ProjectGuid> ");
            builder.AppendLine("    <Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration> ");
            builder.AppendLine("    <Platform Condition=\" '$(Platform)' == '' \">AnyCPU</Platform> ");
            builder.AppendLine("    <ApplicationIcon> ");
            builder.AppendLine("    </ApplicationIcon> ");
            builder.AppendLine("    <AssemblyKeyContainerName> ");
            builder.AppendLine("    </AssemblyKeyContainerName> ");
            builder.AppendLine("    <AssemblyName>" + NameSpace + ".IDAL</AssemblyName> ");
            builder.AppendLine("    <AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    </AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    <DefaultClientScript>JScript</DefaultClientScript> ");
            builder.AppendLine("    <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout> ");
            builder.AppendLine("    <DefaultTargetSchema>IE50</DefaultTargetSchema> ");
            builder.AppendLine("    <DelaySign>false</DelaySign> ");
            builder.AppendLine("    <OutputType>Library</OutputType> ");
            builder.AppendLine("    <RootNamespace>" + NameSpace + ".IDAL</RootNamespace> ");
            builder.AppendLine("    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> ");
            builder.AppendLine("    <StartupObject> ");
            builder.AppendLine("    </StartupObject> ");
            builder.AppendLine("    <FileUpgradeFlags> ");
            builder.AppendLine("    </FileUpgradeFlags> ");
            builder.AppendLine("    <UpgradeBackupLocation> ");
            builder.AppendLine("    </UpgradeBackupLocation> ");
            builder.AppendLine("    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> ");
            builder.AppendLine("    <OldToolsVersion>2.0</OldToolsVersion> ");
            builder.AppendLine("    <PublishUrl>publish\\</PublishUrl> ");
            builder.AppendLine("    <Install>true</Install> ");
            builder.AppendLine("    <InstallFrom>Disk</InstallFrom> ");
            builder.AppendLine("    <UpdateEnabled>false</UpdateEnabled> ");
            builder.AppendLine("    <UpdateMode>Foreground</UpdateMode> ");
            builder.AppendLine("    <UpdateInterval>7</UpdateInterval> ");
            builder.AppendLine("    <UpdateIntervalUnits>Days</UpdateIntervalUnits> ");
            builder.AppendLine("    <UpdatePeriodically>false</UpdatePeriodically> ");
            builder.AppendLine("    <UpdateRequired>false</UpdateRequired> ");
            builder.AppendLine("    <MapFileExtensions>true</MapFileExtensions> ");
            builder.AppendLine("    <ApplicationRevision>0</ApplicationRevision> ");
            builder.AppendLine("    <ApplicationVersion>1.0.0.%2a</ApplicationVersion> ");
            builder.AppendLine("    <IsWebBootstrapper>false</IsWebBootstrapper> ");
            builder.AppendLine("    <UseApplicationTrust>false</UseApplicationTrust> ");
            builder.AppendLine("    <BootstrapperEnabled>true</BootstrapperEnabled> ");
            builder.AppendLine("    <TargetFrameworkProfile /> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Debug\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>DEBUG;TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>true</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>false</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>full</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Release\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>false</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>true</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>none</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <Reference Include=\"System\"> ");
            builder.AppendLine("      <Name>System</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Data\"> ");
            builder.AppendLine("      <Name>System.Data</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Xml\"> ");
            builder.AppendLine("      <Name>System.XML</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <ProjectReference Include=\"..\\Model\\" + NameSpace + ".Model.csproj\"> ");
            builder.AppendLine("      <Name>" + NameSpace + ".Model</Name> ");
            builder.AppendLine("    </ProjectReference> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);
                builder.AppendLine("    <Compile Include=\"" + className + ".cs\" /> ");
            }
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Client.3.5\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName> ");
            builder.AppendLine("      <Install>false</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Framework.3.5.SP1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Windows.Installer.3.1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>Windows Installer 3.1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <Import Project=\"$(MSBuildBinPath)\\Microsoft.CSharp.targets\" /> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <PreBuildEvent> ");
            builder.AppendLine("    </PreBuildEvent> ");
            builder.AppendLine("    <PostBuildEvent> ");
            builder.AppendLine("    </PostBuildEvent> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("</Project> ");
            string filename = CodeCommon.DesktopDirectory + NameSpace + @"/IDAL/" + NameSpace + ".IDAL.csproj";
            CodeCommon.CreateFiles(filename, builder.ToString());
        }
        #endregion
    }
}
